package org.osami.androidui;

import org.osami.utils.Logger;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;


class WebConsole{
	Logger log = new Logger("WebView");
	public void log(String msg){
		log.d(msg);
	}
}

public class ContentFragment extends Fragment {
	
	private WebView webview;

	//private ImageView waitImage;
	private RelativeLayout layout;
	private ProgressBar progressBar;
	
	/*
	protected void startAnimation() {
	  // Create an animation
	  RotateAnimation rotation = new RotateAnimation(
	      0f,
	      360f,
	      Animation.RELATIVE_TO_SELF,
	      0.5f,
	      Animation.RELATIVE_TO_SELF,
	      0.5f);
	  rotation.setDuration(1200);
	  rotation.setInterpolator(new LinearInterpolator());
	  rotation.setRepeatMode(Animation.RESTART);
	  rotation.setRepeatCount(Animation.INFINITE);

	  waitImage.startAnimation(rotation);
	  
	}
	 */
	public void serviceChanged(OsamiServiceInfo service){
		layout.removeAllViewsInLayout();
		layout.addView(progressBar);
				   
		//startAnimation();
		webview.loadUrl(service.getUiUrl());		
	}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
	{
		Context context = this.getActivity().getBaseContext();

		layout = new RelativeLayout(context);
		
		//waitImage = new ImageView(context);
		//waitImage.setImageResource(R.drawable.icon);
		progressBar = new ProgressBar(context);
		progressBar.setIndeterminate(true);
		
		RelativeLayout.LayoutParams params;
		params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);					
		params.addRule(RelativeLayout.CENTER_HORIZONTAL);
		params.addRule(RelativeLayout.CENTER_VERTICAL);
		progressBar.setLayoutParams(params);
		
		final OsamiUI activity = (OsamiUI)getActivity();
		activity.contentFragment = this;
		
		webview = new WebView(this.getActivity());
		params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		webview.setLayoutParams(params);
		layout.setLayoutParams(params);
		
		webview.setWebChromeClient(new WebChromeClient() {
		   public void onProgressChanged(WebView view, int progress) {
			   
			   if(progress >= 100){
				   layout.removeAllViewsInLayout();
				   layout.addView(webview);
				   //layout.removeView(progressBar);
			   }
		   }
		   @Override
			public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
				return super.onConsoleMessage(consoleMessage);
			}
		   
		   /*
		   @Override
		   public boolean onJsAlert(WebView view, String url, String message,
					JsResult result) 
		   {
			   Toast.makeText(getActivity().getBaseContext(),message,Toast.LENGTH_SHORT).show();			   
			
			   return true;
		   }
		   */
		});
		
		webview.setWebViewClient(new WebViewClient(){
			// Don't start browser for links, open within.
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				layout.removeAllViewsInLayout();
				layout.addView(progressBar);
				
				view.loadUrl(url);//"http://www.google.com");
				return true;
			}
		});
		
		webview.addJavascriptInterface(new OsgiRelay(webview), "_OsgiRelay");
		webview.addJavascriptInterface(new WebConsole(), "console");
		
		webview.getSettings().setJavaScriptEnabled(true);
		webview.getSettings().setDomStorageEnabled(true);
		// Too much trouble updating javascripts with cache enabled
		//webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
		
		String initial = "<html><style>body{background:black;color:white}</style><body align='center'>Please choose a service.</body></html>";
		webview.loadData(initial, "text/html", "utf-8");
		
		return layout;
    }
}
